﻿module PreprocessorTests

open Preprocessor
open Preprocessor.Preprocessor
open Lexer
open NUnit.Framework
open Clexer
open Preprocessor.Prep

[<TestFixture>]
type PreprocessorTest () =

    let runTest filename correctFile= 
        let check first second = 
            Seq.compareWith (fun (x:string) (y:string) -> if x=y then 0 else 2) first second 
        let lexemes = runLexer filename
        let prep = new Prep(System.IO.Path.GetFileName(filename))
        let context = new Context()
        let result = List.ofSeq (Seq.map PrintUtils.TokenToString (preprocessNext context prep lexemes))
        let correctResult = List.ofSeq (Seq.map PrintUtils.TokenToString 
                                                (Seq.filter (fun x -> match x with | token.EOF(_) -> false | _ -> true) (runLexer correctFile)))
        Assert.AreEqual(check result correctResult, 0) 

    [<Test>]
    member test.DefineTest () = 
        runTest "../../../Tests/Tested/define.c" "../../../Tests/Correct/define.c"

    [<Test>]
    member test.UndefTest () = 
        runTest "../../../Tests/Tested/undef.c" "../../../Tests/Correct/undef.c"

    [<Test>]
    member test.IfdefTest () = 
        runTest "../../../Tests/Tested/ifdef.c" "../../../Tests/Correct/ifdef.c"

    [<Test>]
    member test.IncludeTest () = 
        runTest "../../../Tests/Tested/includetest.c" "../../../Tests/Correct/includetest.c"

[<EntryPoint>]
let main _ =
    let test = new PreprocessorTest()
    test.DefineTest()
    test.IfdefTest()
    test.UndefTest()
    //test.IncludeTest()
    0
